<?php
namespace app\admin\controller;

use app\admin\library\AdminController;
use app\admin\library\User;
use cubox\Cubox;
use cubox\Check;

class My extends AdminController{
    public function onLogout(){
        User::logout();
        $this->go($this->app->url('outside','login'));
    }

    public function onModifyPassword(){

         
        $old_password = $this->post('old_password',$this->check('就密码')->required());
        $new_password = $this->post('new_password',$this->check('新密码')->required());

        $user_id = User::get('user_id');

        $user = $this->model('User');

        $oldUserData = $user->getId($user_id);

        if (!$oldUserData){
            throw new \Exception('用户不存在');
        }

        if ($oldUserData['password'] != User::makePassword($old_password) ){
            throw new \Exception('旧密码错误');
        }

        if ($oldUserData['password'] == User::makePassword($new_password)){
            throw new \Exception('新密码不能与旧密码相同');
        }

        $update = [
            'password' => User::makePassword($new_password)
        ];

        if ($oldUserData['status']=='new') {
            $update['status'] = 'normal';
        }

        if ($user->updateId($user_id,$update)){

            $this->log('修改密码');
            return ;
        }else{
            throw new \Exception('修改失败');
        }
    }
}